﻿using System;
using System.Management;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a Service ManagementObject to NetworkInterface Class
    /// </summary>
    public static class NetworkInterfaceTranslate
    {
        /// <summary>
        /// Convert Service ManagementObject to NetworkInterface Class
        /// </summary>
        /// <param name="clsnet">NetworkInterface class</param>
        /// <param name="queryObj">ManagementObject Group</param>
        /// <returns>NetworkInterface class with properties converted</returns>
        public static NetworkInterfaceProperty Convert(this NetworkInterfaceProperty clsnet, ManagementObject queryObj)
        {
            clsnet.Adapter = queryObj.PropertyValue<string>("Adapter");
            clsnet.AdapterId = queryObj.PropertyValue<string>("AdapterId");
            clsnet.Address = queryObj.PropertyValue<string>("Address");
            clsnet.Availability = queryObj.PropertyEnum<UInt16, AvailabilityNetwork>("Availability", AvailabilityNetwork.Unknown);
            clsnet.Caption = queryObj.PropertyValue<string>("Caption");
            clsnet.Characteristics = queryObj.PropertyEnum<UInt32, CharacteristisNetwork>("Characteristics", CharacteristisNetwork.Unknown);
            clsnet.ConfigManagerErrorCode = queryObj.PropertyEnum<UInt32, ConfigManagerErrorCode>("ConfigManagerErrorCode", ConfigManagerErrorCode.Unknown);
            clsnet.ConfigManagerUserConfig = queryObj.PropertyValue<bool>("ConfigManagerUserConfig");
            clsnet.CreationClassName = queryObj.PropertyValue<string>("CreationClassName");
            clsnet.Description = queryObj.PropertyValue<string>("Description");
            clsnet.DeviceId = queryObj.PropertyValue<string>("DeviceId");
            clsnet.DhcpEnabled = queryObj.PropertyValue<bool?>("DhcpEnabled");
            clsnet.ErrorCleared = queryObj.PropertyValue<bool>("ErrorCleared");
            clsnet.ErrorDescription = queryObj.PropertyValue<string>("ErrorDescription");
            clsnet.Flags = queryObj.PropertyValue<UInt32>("Flags");
            clsnet.IdentifyingDescriptions = queryObj.PropertyValue<string[]>("IdentifyingDescriptions");
            clsnet.InstallDate = queryObj.PropertyValue<DateTime?>("InstallDate");
            clsnet.Ipv4Addresses = queryObj.PropertyValue<string[]>("Ipv4Addresses");
            clsnet.Ipv6Addresses = queryObj.PropertyValue<string[]>("Ipv6Addresses");
            clsnet.LastErrorCode = queryObj.PropertyValue<UInt32>("LastErrorCode");
            clsnet.Name = queryObj.PropertyValue<string>("Name");
            clsnet.Network = queryObj.PropertyValue<string>("Network");
            clsnet.Node = queryObj.PropertyValue<string>("Node");
            clsnet.OtherIdentifyingInfo = queryObj.PropertyValue<string[]>("OtherIdentifyingInfo");
            clsnet.PnpDeviceId = queryObj.PropertyValue<string>("PnpDeviceId");
            clsnet.PowerManagementCapabilities = queryObj.PropertyArrayEnum<UInt16, PowerCapacity>("PowerManagementCapabilities");
            clsnet.PowerManagementSupported = queryObj.PropertyValue<bool>("PowerManagementSupported");
            clsnet.PowerOnHours = queryObj.PropertyValue<UInt64>("PowerOnHours");
            clsnet.PrivateProperties = new PrivatePropertiesData(queryObj);
            clsnet.State = queryObj.PropertyEnum<UInt32, StateNetworkInterface>("State", StateNetworkInterface.StateUnknown);
            clsnet.Status = queryObj.PropertyEnum<string, StatusResource>("Status", StatusResource.Unknown);
            clsnet.StatusInfo = queryObj.PropertyEnum<UInt32, StatusInfoNetwork>("StatusInfo", StatusInfoNetwork.Unknown);
            clsnet.SystemCreationClassName = queryObj.PropertyValue<string>("SystemCreationClassName");
            clsnet.SystemName = queryObj.PropertyValue<string>("SystemName");
            clsnet.TotalPowerOnHours = queryObj.PropertyValue<UInt64>("TotalPowerOnHours");
            return clsnet;
        }

        ///<summary>
        /// Convert Network class to ManagementObject and Save on Cluster
        ///</summary>
        ///<param name="clsnet">Network class to converter</param>
        ///<param name="opSystem">Operation System where cluster is running</param>
        public static void SaveManagementObject(this NetworkInterfaceProperty clsnet, WindowsOperationSystem opSystem)
        {
            clsnet.Instance["Characteristics"] = (UInt32)clsnet.Characteristics;
            clsnet.Instance["Description"] = clsnet.Description;
            clsnet.Instance["Flags"] = clsnet.Flags;
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsnet.Instance["PrivateProperties"] = clsnet.PrivateProperties.ConvertToManagemenBasetObject();
            }
            clsnet.Instance.Put();
        }
    }
}
